# Contributor: Oleg Titov <oleg.titov@gmail.com>
# Contributor: Jesse Chan <jc@linux.com>
# Maintainer: Jesse Chan <jc@linux.com>
pkgname=bazel4
pkgver=4.2.2
pkgrel=1
pkgdesc="A fast, scalable, multi-language and extensible build system"
url="https://bazel.build/"
arch="all"
arch="$arch !x86 !armhf !armv7" # JDK dropped support
arch="$arch !riscv64" # JDK not yet bootstrapped
arch="$arch !ppc64le" # absl: Must implement UnscaledCycleClock::Frequency()
arch="$arch !s390x" # aborted: SIGFPE (0x8) at pc=0x000003ffb1f18462 (sent by kill)
license="Apache-2.0"
depends="bash java-jdk libarchive unzip zip"
makedepends="coreutils linux-headers openjdk17 protobuf python3"
options="!strip"
provides="bazel"
subpackages="
	$pkgname-doc
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	"
source="https://github.com/bazelbuild/bazel/releases/download/$pkgver/bazel-$pkgver-dist.zip
	patch_do_not_use_glibc_prebuilt.patch
	patch_default_system_java_toolchain.patch
	patch_fix_jdk_13.patch
	patch_stop_hardcoding_verify_none_1.patch
	patch_stop_hardcoding_verify_none_2.patch
	patch_ftbfs_gcc11_1.patch
	patch_ftbfs_gcc11_2.patch
	patch_ftbfs_gcc11_3.patch
	patch_ftbfs_gcc11_4.patch
	patch_ftbfs_gcc11_5.patch
	patch_remove_illegal_reflective.patch"

build() {
	export JAVA_HOME="/usr/lib/jvm/default-jvm"

	# Target Java version (minimum to run)
	# should be the same as the upstream
	export JAVA_VERSION="1.8"

	# horrible of horribles, just to have python in the PATH
	# https://github.com/bazelbuild/bazel/issues/8665
	mkdir -p ./bin-hack
	ln -s /usr/bin/python3 ./bin-hack/python
	export PATH=$(pwd)/bin-hack:$PATH

	# Stage 1: build Bazel with bootstrap script

	# based on
	# https://github.com/vbatts/copr-build-bazel/blob/master/bazel.spec
	# EXTRA_BAZEL_ARGS is used by bootstrap script only
	export EXTRA_BAZEL_ARGS="--sandbox_debug --verbose_failures"

	mkdir tmphome-stage1

	env HOME="$(pwd)/tmphome-stage1" bash ./compile.sh
	env HOME="$(pwd)/tmphome-stage1" ./output/bazel shutdown

	# Stage 2: use Bazel to build Bazel in optimized mode
	mkdir tmphome-stage2

	# We can't use embedded JDK, as there is no alpine support
	echo startup --server_javabase=$JAVA_HOME \
		>> tmphome-stage2/.bazelrc

	env HOME="$(pwd)/tmphome-stage2" ./output/bazel build \
		-c opt \
		--stamp \
		--embed_label $pkgver \
		--distdir=derived/distdir \
		--javacopt="-source $JAVA_VERSION -target $JAVA_VERSION" \
		$EXTRA_BAZEL_ARGS \
		//scripts/packages:without-jdk/install.sh
	env HOME="$(pwd)/tmphome-stage2" ./output/bazel shutdown

	# Copy outputs
	rm -rf output && mkdir output
	cp bazel-bin/scripts/packages/without-jdk/bazel output
	cp bazel-bin/scripts/packages/without-jdk/bazel-real output
	cp bazel-bin/scripts/bazel-complete.bash output
	cp bazel-bin/scripts/bazel.fish output
	cp scripts/zsh_completion/_bazel output
	cp tmphome-stage2/.bazelrc output/bazel.bazelrc
}

check() {
	mkdir tmphome-check
	cp output/bazel.bazelrc tmphome-check/.bazelrc

	env HOME="$(pwd)/tmphome-check" ./output/bazel test \
		--distdir=derived/distdir \
		--spawn_strategy=standalone \
		--genrule_strategy=standalone \
		--verbose_failures \
		--verbose_test_summary \
		--test_verbose_timeout_warnings \
		examples/cpp:hello-success_test \
		examples/py_native:test \
		examples/java-starlark/src/test/java/com/example/myproject:pass \
		examples/java-native/src/test/java/com/example/myproject:hello

	env HOME="$(pwd)/tmphome-check" ./output/bazel shutdown
}

package() {
	install -Dm755 output/bazel "$pkgdir"/usr/bin/bazel
	install -Dm755 output/bazel-real "$pkgdir"/usr/bin/bazel-real
	install -Dm644 output/bazel.bazelrc \
		"$pkgdir"/etc/bazel.bazelrc

	install -m644 -D -t "$pkgdir"/usr/share/doc/$pkgname README.md

	install -Dm644 output/bazel-complete.bash \
		"$pkgdir"/usr/share/bash-completion/completions/"$pkgname"
	install -Dm644 output/bazel.fish \
		"$pkgdir"/usr/share/fish/completions/"$pkgname".fish
	install -Dm644 output/_bazel \
		"$pkgdir"/usr/share/zsh/site-functions/_"$pkgname"
}

unpack() {
	mkdir -p "$builddir"
	unzip "$srcdir"/bazel-$pkgver-dist.zip -d "$builddir" > /dev/null
}

sha512sums="
123f73dc87053e37705bb729f82bd722d6d2799fe106f79c51cf5566fb2771d824108cbe275aad55ae590b970c549008b433704cbf1245394769e950796eb8db  bazel-4.2.2-dist.zip
b8858494f7c600145610d44d2ca81d2cf0e58924b5d876361043b395c88b1166bf9a17de2864ee718e6dce723b7f0c4d71cf8fb9c2ab316eb9013eb5e450ddbe  patch_do_not_use_glibc_prebuilt.patch
fe1862bbd392f8a2d7af5f7c5e08fa6cf6dab0ba7b88280c28c4b072241613480e861af60212ceee1aebc10eb629f964c5def2b3e0a75c48453da8c397f4e93b  patch_default_system_java_toolchain.patch
0b4b3e541eaa496253e83d6fd88ba98d14c2a53324b7438f57d240b45f2c3c63b659038224e2b66706d4420eccb7c17c834260839f2fd39e3c6b3dea2613b735  patch_fix_jdk_13.patch
f8456643419ac108a7cd72eaccd931e9c269c828611b0a62a79ad80a4f542b83cf5fa69af026e04bfb8d9f2c70527b5b25f9af56edb0a5a696ce66b3032df48d  patch_stop_hardcoding_verify_none_1.patch
c5ad25926e5672cd86b27c791c7347b21b57777296cc26d92d2bd9ae6eb39ee81d48008edb666285432135082e91daa9162ea67c719625bcede8f2d5647c9633  patch_stop_hardcoding_verify_none_2.patch
992cf1acd662fd7e5f418e15122fd957c69bd6ca030b782b82d34222bec908d408472e839843b51fe828c2de5e56f4ff4cb4b4adcc17dacf82f2db7e9950cd64  patch_ftbfs_gcc11_1.patch
e9d6e4642a25ce5323ba00e75172238fe52dc31e39ac91e97b5aae6deb9661c5426d763584e1b1b9ee489204e3bf891d84e14e7fa339c9a7a17a5a67c9129dc3  patch_ftbfs_gcc11_2.patch
a001b7fe0bc7b34a6aab73e7f88a2f52dc5e67fcf3dda15bc06a6a8709b6ffa8883f705eb22ef83705fe70dfdbef64c5ff8ba762ffb526366b033375501e05e9  patch_ftbfs_gcc11_3.patch
b83b73b5ea7a2fc99d13567f881dcfac3f242510ab429e33fe4347024fb9fbbd464fb7ef7ee4d380452b17c459d84494f993ba134db65c13f2a27ae475a5d8cc  patch_ftbfs_gcc11_4.patch
750c4ba2f41d61321db36c1d1154744e90d1b0f9ea39c75582edbdc28d16df2cee9c85fc4831114a414b6db8dba7b16d17b272b55a0308ac23a06242fc0c31db  patch_ftbfs_gcc11_5.patch
47377bb1866d95c84e7e5a8709a3d95d424371697385a2e7cb9e2e1f8c50cf4e3e76dcc15eec0303466d999da24faf1682f858010c574a2373f1f077b4691426  patch_remove_illegal_reflective.patch
"
